#include("../../layout/header.html")
#@layout()
#define content()
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入权限名称、菜单、标识等"/>
                        </div>
                    </div>
                    <!--<div class="layui-inline">
                        <label class="layui-form-label">菜单url:</label>
                        <div class="layui-input-inline">
                            <input name="url" class="layui-input" placeholder="输入路由地址"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">权限标识:</label>
                        <div class="layui-input-inline">
                            <input name="authority" class="layui-input" placeholder="输入权限标识"/>
                        </div>
                    </div>-->
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="authoritiesEditDialog">
    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input name="id" type="hidden"/>
        <div class="layui-row">
            <div class="layui-form-item">
                <label class="layui-form-label">上级菜单</label>
                <div class="layui-input-block">
                    <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">权限名称:</label>
                    <div class="layui-input-block">
                        <input name="name" placeholder="请输入权限名称" class="layui-input"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标:</label>
                    <div class="layui-input-block">
                        <!--                        <input id="iconPicker" name="icon" placeholder="请输入菜单图标" class="layui-input"/>-->
                        <input type="text" id="iconPicker" name="icon" lay-filter="iconPicker" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">权限类型:</label>
                <div id="typeDiv" class="layui-input-block">
                    <!--                    <input name="type" type="radio" value="0" title="菜单" checked/>-->
                    <!--                    <input name="type" type="radio" value="1" title="按钮"/>-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单url:</label>
                <div class="layui-input-block">
                    <input name="url" placeholder="请输入菜单url" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限标识:</label>
                <div class="layui-input-block">
                    <input name="authority" placeholder="请输入权限标识" class="layui-input"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">排序号:</label>
                <div class="layui-input-block">
                    <input name="sort" placeholder="请输入排序号" type="number" class="layui-input"
                           lay-verType="tips" lay-verify="required|number" value="1" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect','iconPicker','formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var formX = layui.formX;
        var xmSelect = layui.xmSelect;
        var iconPicker = layui.iconPicker;
        var tbDataList = [],insTb;

        treeRender();
        function treeRender() {
            admin.req('/sys/sysAuthorities/list',{page:1,limit:10000},function (d) {
                if(d.code === 0){
                    tbDataList = d.data;
                    insTb = treeTable.render({
                        elem: '#authoritiesTable',
                        data: tbDataList,
                        toolbar: ['<p>',
                            '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                            '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                            '</p>'].join(''),
                        tree: {
                            iconIndex: 2,
                            idName: 'id',
                            pidName: 'pid',
                            isPidData: true
                        },
                        height: 'full-150',
                        cols: [[
                            {type: 'checkbox'},
                            {type: 'numbers'},
                            {field: 'name', title: '权限名称', minWidth: 150},
                            {
                                title: '类型', templet: function (d) {
                                    return [
                                        '<span class="layui-badge layui-badge-green">目录</span>',
                                        '<span class="layui-badge layui-badge-gray">菜单</span>',
                                        '<span class="layui-badge layui-badge-blue">按钮</span>',
                                        '<span class="layui-badge layui-badge-red">API</span>'
                                    ][d.type - 1];
                                }, align: 'center', width: 80
                            },
                            {
                                title: '菜单图标',
                                templet: '<p><i class="layui-icon {{d.icon}}"></i></p>',
                                align: 'center',
                                width: 100
                            },
                            {field: 'url', title: '菜单url'},
                            {field: 'authority', title: '权限标识'},
                            {field: 'sort', title: '排序号', align: 'center', width: 80},

                            {field: 'createTime', title: '创建时间', align: 'center'},
                            {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', width: 120}
                        ]],
                        done:function (data) {
                            let searchValue = $('input[name=name]').val();
                            if(searchValue){
                                console.log(searchValue)
                                doTbSearch(searchValue)
                            }
                        }
                    });
                }
            })
        }

        var type = [];
        admin.req('/sys/sysDictData/get', {type: 'authority'}, function (d) {
            if (0 === d.code) {
                type = d.data.authority;


            }
        })

        /* 表格操作列点击事件 */
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        treeTable.on('toolbar(authoritiesTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = insTb.checkStatus();
                if (checkRows.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 表格搜索 */
        form.on('submit(authoritiesTbSearch)', function (data) {
            doTbSearch(data.field.name);

            return false;
        });

        /* 添加 */
        $('#authoritiesAddBtn').click(function () {
            showEditModel();
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.popupRight({
                type: 1,
                area: '600px',
                title: (mData ? '修改' : '添加') + '权限',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
                    // 渲染下拉树
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        data: insTb.options.data,
                        initValue: mData ? [mData.pid] : [],
                        model: {label: {type: 'text'}},
                        filterable: true,
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: true
                        }
                    });
                    formX.renderRadio({
                        elem: '#typeDiv',
                        data: type,
                        name: 'type',
                        title: 'name',
                        value: 'value',
                        initValue: mData ? mData.type : "",
                        defaultRadio: false
                    })
                    let tmpIcon;
                    iconPicker.render({
                        // 选择器，推荐使用input
                        elem: '#iconPicker',
                        // 数据类型：fontClass/unicode，推荐使用fontClass
                        type: 'fontClass',
                        // 是否开启搜索：true/false
                        search: true,
                        // 点击回调
                        click: function (data) {
                            if(mData)mData.icon = data.icon;
                            tmpIcon = data.icon;
                            console.log(tmpIcon)
                        }
                    });

                    // 回显表单数据
                    form.val('authoritiesEditForm', mData);
                    iconPicker.checkIcon('iconPicker', mData ? mData.icon : '');

                    // 表单提交事件
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        data.field.pid = insXmSel.getValue('valueStr');
                        data.field.icon =  tmpIcon;
                        var loadIndex = layer.load(2);
                        admin.post('/sys/sysAuthorities/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                treeRender();

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });

                    // 弹窗不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/sysAuthorities/del', {id: obj.data ? obj.data.id : obj.ids}, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        treeRender();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

        /* 搜索表格数据 */
        function doTbSearch(field) {
            if (field) {
                insTb.filterData(field);
            } else {
                insTb.clearFilter();
            }

        }

    });
</script>
</body>
#end
